// 本地存储模块,主要是为了存储搜索历史的
const HISTORY_KEY = "search_history";
const TOPIC_KEY = "topic_history";
// 获取搜索历史
export const getHistory = () => {
  const defaultObj = [];
  const result = JSON.parse(localStorage.getItem(HISTORY_KEY));
  return result || defaultObj;
};
// 设置搜索历史
export const setHistory = (history) => {
  localStorage.setItem(HISTORY_KEY, JSON.stringify(history));
};

// 清除搜索历史
export const clearHistory = () => {
  localStorage.removeItem(HISTORY_KEY);
};

// 获取话题列表
export const getTopicList = () => {
  const defaultObj = [];
  const result = JSON.parse(localStorage.getItem(TOPIC_KEY));
  return result || defaultObj;
};
// 设置话题列表
export const setTopicList = (topicList) => {
  // 原来有话题就直接添加进去
  const oldTopicList = getTopicList();
  topicList = [...oldTopicList, ...topicList];
  localStorage.setItem(TOPIC_KEY, JSON.stringify(topicList));
};
// 清除话题列表
export const clearTopicList = () => {
  localStorage.removeItem(TOPIC_KEY);
};
